local addonName, IWonew = ...
local BlacksmithingDone = false
local EnchantingDone = false
local SLJHBuff = false

-- /dump C_TradeSkillUI.GetProfessionInfoByRecipeID(453727)
-- 拆分物品
-- C_Container.SplitContainerItem(containerIndex, slotIndex, amount)
function Exe_DXT()
  if not SLJHBuff then
    SetSLJHBuff()
    return
  end
  if not BlacksmithingDone then
    CreateBlacksmithing()
    return
  end
  if not EnchantingDone then
    CreateEnchanting()
    return
  end
  IWonew.CreateTextFrame(80, "制造", 240, 0)
  print("制作完成！")
end

function Debug() 

  SalvageXY()
  local itemID = 213610
  IWonew.PrintDebug(IWonew.getRandomString(10))
  local itemSlot = IWonew.GetItemSlotFromAccountBank(13, itemID)
  local emptyLocation = ItemLocation:CreateEmpty()
  
  if itemSlot then
    print(IWonew.TableToString(itemSlot))
    print(IWonew.TableToString(itemSlot.itemLocation))
    emptyLocation.SetBagAndSlot(emptyLocation, itemSlot.bagID, itemSlot.slotID)
    print('2', IWonew.TableToString(emptyLocation))
  end
  local interacting = C_PlayerInteractionManager.IsInteractingWithNpcOfType(Enum.PlayerInteractionType.AccountBanker)
  local isValidInteraction = C_PlayerInteractionManager.IsValidNPCInteraction(Enum.PlayerInteractionType.AccountBanker)
  C_PlayerInteractionManager.ConfirmationInteraction(Enum.PlayerInteractionType.AccountBanker)
  print("interacting:", interacting)
  print("isValidInteraction:", isValidInteraction)
  local result = C_PlayerInteractionManager.InteractUnit("战团银行汇核", true, false)
  print("result", result)
  -- C_PlayerInteractionManager.InteractUnit("218381")
  -- CreateBlacksmithing()
  -- CreateEnchanting()
  print("xxxx", 2333)
  -- C_Bank.DepositMoney(Enum.BankType.Account,100000000)
  print("C_Bank.CanUseBank", C_Bank.CanUseBank(Enum.BankType.Account))
  numSlots, full = GetNumBankSlots()
  print("enabled:", numSlots, full)
  local purchasedBankTabIDs = C_Bank.FetchPurchasedBankTabIDs(Enum.BankType.Account)
  print(IWonew.TableToString(purchasedBankTabIDs))
  print("enabled:0", C_Container.GetContainerNumSlots(0))
  print("enabled:1", C_Container.GetContainerNumSlots(1))
  print("enabled:2", C_Container.GetContainerNumSlots(2))
  print("enabled:3", C_Container.GetContainerNumSlots(3))
  print("enabled:4", C_Container.GetContainerNumSlots(4))
  print("enabled:5", C_Container.GetContainerNumSlots(5))
  print("enabled:13", C_Container.GetContainerNumSlots(13))
  print("enabled:13", C_Container.GetBagName(13))
  print("enabled:13", C_Container.GetContainerItemID(13, 1))
  print("enabled:13", C_Container.GetContainerItemID(14, 1))
  -- local slot = IWonew.GetItemSlot(213610)
  -- print("xxxx", slot.itemLocation)
end

-- 获取碎裂精华buff
function SetSLJHBuff()
  local itemID = 213610
  local itemSlot = IWonew.GetItemSlot(itemID)
  if itemSlot == nil then
    SLJHBuff = true
    return
  end
  local recipeSpellID = 445466
  -- C_TradeSkillUI.CraftSalvage(recipeSpellID, numCasts?, itemTarget [, craftingReagents [, applyConcentration]])
  -- 晶脉粉末的位置
  local result = C_TradeSkillUI.CraftSalvage(recipeSpellID, 1, itemSlot.itemLocation)
  SLJHBuff = true
end


function CreateBlacksmithing()
  local blacksmithingProfessionInfo = C_TradeSkillUI.GetProfessionInfoByRecipeID(453727)
  if blacksmithingProfessionInfo == nil or blacksmithingProfessionInfo.skillLevel < 50 then
    print("未学习锻造技能")
    BlacksmithingDone = true
    return
  end
  local skillLineID = 2872 -- 锻造skillLineID
  local currencyType = C_TradeSkillUI.GetConcentrationCurrencyID(skillLineID)
  local currencyInfo = C_CurrencyInfo.GetCurrencyInfo(currencyType)
  local recipeID = 450219 -- 镔爪合金配方ID
  -- 配方所需的材料
  local craftingReagents = {
    {dataSlotIndex = 1, itemID = 222418, quantity = 2}, -- 2星心核合金
    {dataSlotIndex = 2, itemID = 210937, quantity = 5}, -- 2星镔爪框
  }

  local operationInfo = C_TradeSkillUI.GetCraftingOperationInfo(recipeID, craftingReagents, IWonew.getRandomString(10), false)
  if operationInfo and operationInfo.concentrationCost < currencyInfo.quantity then
    -- C_TradeSkillUI.CraftRecipe(recipeSpellID [, numCasts [, craftingReagents [, recipeLevel [, orderID [, applyConcentration]]]]])
    C_TradeSkillUI.CraftRecipe(recipeID, 1, craftingReagents, nil, nil, true)
    print("能使用专注制作")
    return
  end
  print("不能使用专注制作")
  BlacksmithingDone = true
end

function CreateEnchanting()
  local skillLineID = 2874 -- 附魔skillLineID
  local currencyType = C_TradeSkillUI.GetConcentrationCurrencyID(skillLineID)
  local currencyInfo = C_CurrencyInfo.GetCurrencyInfo(currencyType)
  print("currencyInfo:", currencyInfo.name, currencyInfo.quantity)
  -- 获取附魔羊皮纸位置
  local itemSlot = IWonew.GetItemSlot(38682)
  if itemSlot == nil then
    EnchantingDone = true
    print("没有附魔羊皮纸")
    return
  end
  local dd = 445388
  -- C_TradeSkillUI.CraftEnchant(445381, 1, {}, targetLocation)
  local recipeID = 445333 -- 晶脉辉煌配方ID
  -- 配方所需的材料
  local craftingReagents = {
    {dataSlotIndex = 1, itemID = 219947, quantity = 75}, -- 2星风暴之尘
    {dataSlotIndex = 2, itemID = 219950, quantity = 10}, -- 2星粼光碎片
    {dataSlotIndex = 3, itemID = 219954, quantity = 1}, -- 2星耀辉水晶
  }
  local operationInfo = C_TradeSkillUI.GetCraftingOperationInfo(recipeID, craftingReagents, IWonew.getRandomString(10), false)
  print(IWonew.TableToString(operationInfo))
  if operationInfo and operationInfo.concentrationCost < currencyInfo.quantity then
    -- C_TradeSkillUI.CraftEnchant(recipeSpellID, numCasts, craftingReagents, itemTarget, applyConcentration) end
    C_TradeSkillUI.CraftEnchant(recipeID, 1, craftingReagents, itemSlot.itemLocation, true)
    print("能使用专注制作")
    return
  end
  print("不能使用专注制作")
  EnchantingDone = true
end

-- 制造点火装置
function CreateDhObject()
  local recipeID = 453727 -- 点燃点火装置配方ID
  local craftingReagents = {
    {dataSlotIndex = 1, itemID = 222418, quantity = 1}, -- 2星心核合金
  }
  C_TradeSkillUI.CraftRecipe(recipeID, 1, craftingReagents, nil, nil, false)
end


function CheckBuffs()
  CheckDHBuff()
  CheckQSHJBuff()
end

function CheckDHBuff()
  local currentTime = GetTime()
  -- 判断锻造是否学习
  -- /dump C_TradeSkillUI.GetProfessionInfoByRecipeID(453727)
  local professionInfo = C_TradeSkillUI.GetProfessionInfoByRecipeID(453727)
  -- print("professionInfo---")
  -- print(IWonew.TableToString(professionInfo))
  -- 锻造技能未学习或者技能点小于50不使用点火装置
  if professionInfo == nil or professionInfo.skillLevel < 50 then return end
  local skillLineID = 2872 -- 锻造skillLineID
	local configID = C_ProfSpecs.GetConfigIDForSkillLine(skillLineID)
	local pathID = 99267 -- 恒燃熔炉pathID
  -- {99267, 99266, 99265, 99264} 4个节点
	local nodeInfo = C_Traits.GetNodeInfo(configID, pathID)
  local activeRank = (nodeInfo and nodeInfo.activeRank or 0) - 1
  print("activeRank", activeRank)
  if activeRank < 10 then return end
	-- print("nodeInfo", IWonew.TableToString(nodeInfo))
	-- local description = C_ProfSpecs.GetDescriptionForPath(pathID)
	-- print("description:---", description)
  local aura = C_UnitAuras.GetPlayerAuraBySpellID(453734)
  if aura and ((aura.expirationTime - currentTime) > 200 ) then return end
  IWonew.CreateTextFrame(80, "点火", 80, 0)
end
  
function CheckQSHJBuff()
  local currentTime = GetTime()
  local aura = C_UnitAuras.GetPlayerAuraBySpellID(432306)
  -- print("xxxx", IWonew.TableToString(aura))
  -- if aura then
  --   print("aura.expirationTime", aura.expirationTime)
  --   print("currentTime", currentTime)
  --   print("currentTime1", aura.expirationTime- currentTime)
  -- end
  -- buff 大于300s
  if aura and ((aura.expirationTime - currentTime) > 300 ) then return end
  IWonew.CreateTextFrame(80, "奇思", 160, 0)
end
